package org.example;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;



public class App {
    //主方法
    public static void main(String[] args) {
        ArrayList<User> list = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("欢迎来到学生管理系统！");
            System.out.println("请选择操作：1.登录 2.注册 3.忘记密码 4.退出");
            String choose = sc.next();
            switch (choose) {
                case "1" -> Login(list);
                case "2" -> register(list);
                case "3" -> forgetPassword(list);
                case "4" -> System.exit(0);
                default -> System.out.println("输入有误，请重新输入");
            }
        }
    }
    //忘记密码的解决方法
    private static void forgetPassword(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username=sc.next();
        boolean flag=contains(list,username);
        if(!flag) {
            System.out.println("该用户不存在,请先注册");
            return;
        }
        User user= null;
        while (true) {
            System.out.println("请输入身份证号：");
            String personId=sc.next();
            System.out.println("请输入手机号码：");
            String phoneNumber=sc.next();
            int index=findIndex(list,username);
            user = list.get(index);
            if(!(user.getPersonID().equalsIgnoreCase(personId)&&user.getPhoneNumber().equalsIgnoreCase(phoneNumber))) {
                System.out.println("输入的两个号码不正确，请重新输入");
                continue;
            }else{
                System.out.println("输入正确");
                break;
            }
        }
        while (true) {
            System.out.println("请输入新的密码：");
            String newpassword=sc.next();
            System.out.println("请再次输入新的密码：");
            String againPassword=sc.next();
            if(newpassword.equals(againPassword)){
                user.setPassword(newpassword);
                System.out.println("密码修改成功");
                break;
            }else{
                System.out.println("两次密码不一致，请重新输入：");
                continue;
            }
        }

    }
    //查找用户名的索引
    private static int findIndex(ArrayList<User> list, String username) {
        for (int i = 0; i < list.size(); i++) {
            User u=list.get(i);
            if(u.getUsername().equals(username)){
                return i;
            }
        }
        return -1;
    }
    //登录方法
    private static void Login(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入用户名：");
            String username = sc.next();
            boolean flag = contains(list, username);
            if (!flag) {
                System.out.println("用户还没注册，请先注册再登录");
                return;
            }
            System.out.println("请输入密码：");
            String password = sc.next();

            while (true) {
                String rightCode = getCode();
                System.out.println("正确验证码为：" + rightCode);
                System.out.println("请输入验证码：");
                String printCode = sc.next();
                if (printCode.equalsIgnoreCase(rightCode)) {
                    System.out.println("验证码正确");
                    break;
                } else {
                    System.out.println("验证码错误，请重新输入");
                    continue;
                }
            }
            User useInfo = new User(username, password, null, null);
            boolean result = checkUserInfo(list, useInfo);
            if (result) {
                System.out.println("登录成功");
                //登录成功后进入学生管理系统
                StudentSystem ss=new StudentSystem();
                ss.startStudentSystem();
                break;
            } else {
                System.out.println("用户名或密码错误");
                if (i == 2) {
                    System.out.println("该号被锁定");
                } else {
                    System.out.println("你还有"+(2-i)+"机会");
                }
            }
        }
    }
//检查用户名和密码是否正确
    private static boolean checkUserInfo(ArrayList<User> list, User useInfo) {
        for (int i = 0; i < list.size(); i++) {
            User u = list.get(i);
            if(u.getUsername().equals(useInfo.getUsername())&&u.getPassword().equals(useInfo.getPassword())){
                return true;
            }
        }
        return false;

    }
//注册方法
    private static void register(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
        String username;
        String password;
        String personID;
        String phoneNumber;
        //输入和校验用户名
        while (true) {
            System.out.println("请输入用户名：");
            username = sc.next();
            boolean flag1 = cherkUsername(username);
            if(!(flag1)){
                System.out.println("用户名不符合要求");
                continue;
            }
            boolean flag2 = contains(list,username);
            if(flag2){
                System.out.println("用户名"+username+"已存在,请重新输入");
                continue;
            }else{
                System.out.println("用户名"+username+"能使用");
                break;
            }
        }
        //输入和校验密码
        while (true) {
            System.out.println("请输入密码：");
            password = sc.next();
            System.out.println("请再次输入密码：");
            String againpassword=sc.next();
            if(password.equals(againpassword)){
                System.out.println("注册成功");
                break;
            }else {
                System.out.println("两次密码不一致,请重新输入：");
                continue;
            }
        }
        //输入身份证号
        while (true) {
            System.out.println("请输入身份证号：");
            personID=sc.next()  ;
            boolean  flag=cherkPersonID(personID);
            if(!(flag)){
                System.out.println("身份证号不符合要求");
                continue;
            }else{
                System.out.println("身份证号"+personID+"能使用");
                break;
            }
        }
        //输入手机号码
        while (true) {
            System.out.println("输入手机号码:");
            phoneNumber=sc.next();
            boolean flag3=cherkPhoneNumber(phoneNumber);
            if(flag3){
                System.out.println("手机号码"+phoneNumber+"能使用");
                break;
            }else{
                System.out.println("手机号码"+phoneNumber+"不符合要求");
                continue;
            }
        }
        User u=new User(username,password,personID,phoneNumber);
        list.add(u);
        printUser(list);

    }
//输出用户信息
    private static void printUser(ArrayList<User> list) {
        for (int i = 0; i < list.size(); i++) {
            User u= list.get(i);
            System.out.println("用户名"+u.getUsername()+"\t"+u.getPassword()+
                    "\t"+u.getPersonID()+"\t"+u.getPhoneNumber());
        }
    }
//校验手机号码
    private static boolean cherkPhoneNumber(String phoneNumber) {
        if (phoneNumber.length() != 11) {
            return false;
        }
        char a=phoneNumber.charAt(0);
        if(a=='0'){
            return false;
        }
        for (int i = 0; i < phoneNumber.length(); i++) {
            char number=phoneNumber.charAt(i);
            if(!(number>='0'&&number<='9')){
                return false;
            }
        }
         return true;
    }
//校验身份证号
    private static boolean cherkPersonID(String personID) {
            if(personID.length()!=18){
                return false;
            }
            char ch=personID.charAt(0);
            if(ch=='0'){
                return false;
            }
            for (int i = 0; i < personID.length()-1; i++) {
                int s=personID.charAt(i);
                if(!(s>='0'&&s<='9')){
                    return false;
                }
            }
            char endChar=personID.charAt(personID.length()-1);
            if(!((endChar>='0'&&endChar<='9')||(endChar=='x')||(endChar=='X'))){
                return false;
            }else {
                return true;
            }
    }
//检查用户名是否存在
    private static boolean contains(ArrayList<User>list, String username){
        for (int i = 0; i < list.size(); i++) {
            String rightname = list.get(i).getUsername();
            if(rightname.equals(username)){
                return true;
            }
        }
        return false;
    }
//校验用户名
    private static boolean cherkUsername(String username) {
        int len = username.length();
        if (len < 3 || len > 15) {
            return false;
        }
        for (int i = 0; i < username.length(); i++) {
            char ch = username.charAt(i);
            if (!((ch >= '0' && ch < '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))) {
                return false;
            }
        }
        int count = 0;
        for (int i = 0; i < username.length(); i++) {
            char ch = username.charAt(i);
            if ((ch >= 'a' && ch <='z') || (ch >='A' && ch <= 'Z')) {
                count++;
                break;
            }
        }
        return count > 0;
    }
//生成验证码
    private static String getCode(){
        ArrayList<Character> list=new ArrayList<>();
        for (int i = 0; i < 26; i++) {
            list.add((char)('a'+i));
            list.add((char)('A'+i));
        }
        Random rd=new Random();
        StringBuilder sb=new StringBuilder();
        //生成4个字母
        for (int i = 0; i < 4; i++) {
            int index=rd.nextInt(list.size());
            char ch=list.get(index);
            sb.append(ch);
        }
        int number=rd.nextInt(10);
        sb.append(number);
        //把数字和字母打乱
        char[]arr=sb.toString().toCharArray();
        int randomIndex=rd.nextInt(arr.length);
        char temp=arr[randomIndex];
        arr[randomIndex]=arr[arr.length-1];
        arr[arr.length-1]=temp;
        //打乱数组
        return new String(arr);

    }

}